<template>
	<view class="serach-container">
		<view class="header">
			<view class="search-box">
				<image src="/static/icon/search.svg"></image>
				<view class="search-input">
					<input type="text" placeholder="请输入关键词搜索" v-model="searchStr" />
				</view>
			</view>
			<view class="search-txt" v-show="goods.length == 0" @click="search()">搜索</view>
			<view class="cancel-txt" v-show="goods.length > 0" @click="cancel()">取消</view>
		</view>
		<view class="content" v-if="goods.length == 0">
			<view class="history" v-if="historyData.length > 0">
				<view class="history-top">
					<text>历史搜索</text>
					<image src="/static/icon/del.png" @click="del()"></image>
				</view>
				<view class="history-bottom">
					<view class="history-item" v-for="(item, index) in historyData" :key="index" @click="copy(item)">
						<text class="hot-city-text">{{ item }}</text>
					</view>
				</view>
			</view>
		</view>
		<view class="content" v-if="goods.length > 0">
			<view class="base-title">
				<text>搜索结果</text>
			</view>
			<scroll-view class="service-list" scroll-y="true">
				<view v-for="(service, index) in goods" :key="index" class="service-item" @click="toDetail(service)">
					<!-- 服务图片 -->
					<view class="service-image-container">
						<image :src="service.post_url || '/static/image/default_service.png'" class="service-image"
							mode="aspectFill"></image>
						<view class="service-status available">
							空闲
						</view>
					</view>

					<!-- 服务信息 -->
					<view class="service-info">
						<view class="service-title">{{ service.title }}</view>
						<view class="service-desc">{{ service.description || '公共信息显示文案最多这么多字数字数字数字数...' }}</view>

						<view class="service-stats">
							<view class="stats-left">
								<view class="like-stats">
									<image src="/static/image/icon_like_default.png" class="stats-icon"
										mode="aspectFit"></image>
									<text class="stats-text">500</text>
								</view>
								<view class="rating-stats">
									<image src="/static/image/icon_thumbs_up.png" class="stats-icon" mode="aspectFit">
									</image>
									<text class="stats-text">95%</text>
								</view>
							</view>
							<view class="distance">
								<text class="distance-text">距离: 500米</text>
							</view>
						</view>
					</view>
				</view>
			</scroll-view>
		</view>
		<view class="content" v-if="goodsRecommend.length > 0">
			<view class="base-title">
				<text>推荐工坊</text>
			</view>
			<scroll-view class="service-list" scroll-y="true">
				<view v-for="(service, index) in goodsRecommend" :key="index" class="service-item" @click="toDetail(service)">
					<!-- 服务图片 -->
					<view class="service-image-container">
						<image :src="service.post_url || '/static/image/default_service.png'" class="service-image"
							mode="aspectFill"></image>
						<view class="service-status available">
							空闲
						</view>
					</view>

					<!-- 服务信息 -->
					<view class="service-info">
						<view class="service-title">{{ service.title }}</view>
						<view class="service-desc">{{ service.description || '公共信息显示文案最多这么多字数字数字数字数...' }}</view>

						<view class="service-stats">
							<view class="stats-left">
								<view class="like-stats">
									<image src="/static/image/icon_like_default.png" class="stats-icon"
										mode="aspectFit"></image>
									<text class="stats-text">500</text>
								</view>
								<view class="rating-stats">
									<image src="/static/image/icon_thumbs_up.png" class="stats-icon" mode="aspectFit">
									</image>
									<text class="stats-text">95%</text>
								</view>
							</view>
							<view class="distance">
								<text class="distance-text">距离: 500米</text>
							</view>
						</view>
					</view>
				</view>
			</scroll-view>
		</view>
	</view>
</template>

<script>
export default {
	data() {
		return {
			searchStr: '',
			historyData: [],
			hotData: [
			],
			goods: [
			],
			goodsData: [
			],
			goodsRecommend: []
		};
	},
	onShow() {
		this.historyData = uni.getStorageSync('historyData') || [
		]
	},
	methods: {
		// 返回首页
		back() {
			uni.navigateBack();
		},
		// 复制文字
		copy(value) {
			this.searchStr = value;
		},
		toDetail(service) {
			uni.navigateTo({
				url: '/pages/store/detail?id=' + service.id
			})
		},
		// 删除历史搜索
		del() {
			let that = this;
			this.$utils.showConfirm("确定要删除历史搜索吗？").then(res => {
				if (res.confirm) {
					that.historyData = [];
				}
			});
		},
		// 搜索商品
		search() {
			let that = this;
			uni.showLoading({
				title: '搜索中...'
			})
			this.$api.getProductList({
				title: this.searchStr,
				pageNo: 1,
				pageSize: 99999
			}).then(res => {
				that.goods = res.rows;
				// 添加至历史搜索
				if (that.historyData.indexOf(that.searchStr) === -1) {
					// 头部插入
					that.historyData.unshift(that.searchStr);
					uni.setStorageSync('historyData', that.historyData);
				}
			})
		},
		cancel() {
			this.goods = [];
		}
	}
}
</script>

<style lang="scss">
page {
	height: 100vh;
	overflow: hidden;
	background-color: #f5f5f5;
}

.serach-container {
	height: 100%;
	display: flex;
	flex-direction: column;

	.content {
		padding: 30rpx 0;
		padding-top: 0;
		background-color: #f5f5f5;
	}

	.header {
		display: flex;
		align-items: center;
		justify-content: space-between;
		padding: 30rpx 30rpx;
		background: #fff;

		.search-box {
			width: 90%;
			background: #F5F5F5;
			border-radius: 42rpx;
			display: flex;
			align-items: center;
			padding: 24rpx 40rpx;

			.search-input {
				margin-left: 20rpx;

				input {
					color: #999999;
					font-size: 28rpx;
				}
			}

			image {
				width: 32rpx;
				height: 32rpx;
			}
		}

		.search-txt {
			min-width: 60rpx;
			font-size: 28rpx;
			color: #666666;
		}

		.cancel-txt {
			min-width: 60rpx;
			font-size: 28rpx;
			color: #666666;
		}
	}

	.base-title {
		padding: 32rpx 34rpx;
		display: flex;
		align-items: center;
		justify-content: space-between;

		text {
			font-size: 32rpx;
			color: #000;
		}
	}

	.history {
		background-color: #fff;
		margin-bottom: 70rpx;
		padding: 32rpx;
		padding-top: 12rpx;
		.history-top {
			display: flex;
			align-items: center;
			justify-content: space-between;
			margin-bottom: 24rpx;

			text {
				font-size: 30rpx;
				color: #333333;
			}

			image {
				flex-shrink: 0;
				width: 32rpx;
				height: 30rpx;
			}
		}

		.history-bottom {
			display: flex;
			flex-wrap: wrap;
			gap: 40rpx;

			.history-item {

				min-width: 140rpx;
				min-height: 56rpx;
				opacity: 1;
				border-radius: 8rpx;
				background: #EDEDED;
				background-color: #f5f5f5;
				display: flex;
				align-items: center;
				justify-content: center;

				padding: 0 12rpx;

				.hot-city-text {
					font-size: 28rpx;
					color: #333;
				}

			}
		}
	}

	.hot {
		.hot-top {

			text {
				font-size: 30rpx;
				font-weight: 600;
				color: #333333;
			}
		}

		.hot-bottom {
			display: flex;
			flex-wrap: wrap;
			margin-top: 20rpx;

			.hot-item {
				border-radius: 28rpx;
				background: #F5F5F5;
				padding: 10rpx 28rpx;
				margin-right: 20rpx;
				margin-top: 24rpx;

				text {
					font-size: 22rpx;
					color: #666666;
				}
			}
		}
	}

	// 服务列表
	.service-list {
		flex-grow: 1;
		width: 100%;
		box-sizing: border-box;
	}

	.service-item {
		display: flex;
		background-color: #fff;
		border-radius: 16rpx;
		padding: 24rpx;
		margin-bottom: 24rpx;
		margin: 0 32rpx 24rpx 32rpx;

		&:active {
			background-color: #f9f9f9;
		}
	}

	.service-image-container {
		position: relative;
		margin-right: 24rpx;

		.service-image {
			width: 128rpx;
			height: 128rpx;
			border-radius: 6rpx;
		}

		.service-status {
			position: absolute;
			top: 2rpx;
			right: 2rpx;
			padding: 6rpx 12rpx;
			border-radius: 20rpx;
			font-size: 20rpx;
			color: #fff;

			&.available {
				background-color: #52c41a;
			}

			&.busy {
				background-color: #ff4d4f;
			}

			&.offline {
				background-color: #d9d9d9;
			}
		}
	}

	.service-info {
		flex: 1;
		display: flex;
		flex-direction: column;
		justify-content: space-between;

		.service-title {
			font-size: 32rpx;
			color: #333;
			font-weight: 500;
			line-height: 1.4;
		}

		.service-desc {
			font-size: 24rpx;
			color: #999;
			line-height: 1.4;
			margin-bottom: 20rpx;
			overflow: hidden;
			text-overflow: ellipsis;
			display: -webkit-box;
			-webkit-line-clamp: 2;
			-webkit-box-orient: vertical;
		}

		.service-stats {
			display: flex;
			justify-content: space-between;
			align-items: center;
		}

		.stats-left {
			display: flex;
			align-items: center;
			gap: 24rpx;
		}

		.like-stats,
		.rating-stats {
			display: flex;
			align-items: center;
			gap: 8rpx;
		}

		.stats-icon {
			width: 28rpx;
			height: 28rpx;
		}

		.stats-text {
			font-size: 24rpx;
			color: #666;
		}

		.distance {
			.distance-text {
				font-size: 24rpx;
				color: #999;
			}
		}
	}
}
</style>
